#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2018/1/22 0022 14:03
# @Author  : Arliki
# @email   : hkdnxycz@outlook.com
# @File    : shetu

import scrapy
import time
import random
import re
from getpic.items import ShetuItem

pic_num = 0


class ShetuSpider(scrapy.Spider):
    name = "shetu"
    allowde_domains = [
        "699pic.com"
    ]
    start_urls = [
        "http://699pic.com/nature.html"
    ]

    def parse(self, response):
        print("开始获取摄图网信息")
        for box in response.xpath("//div[@class='list']"):
            global pic_num
            pic_num += 1
            item = ShetuItem()
            item['url'] = box.xpath("a/img/@data-original")[0].extract()
            item['name'] = box.xpath("a/@title")[0].extract().strip()
            item['alt'] = box.xpath("a/img/@alt")[0].extract()[:-4]
            url = box.xpath("a/@href")[0].extract()
            yield scrapy.Request(url, callback=self.pic_detail, meta=item)
        print("当前页爬取共%s张图片" % pic_num)
        try:
            next_page = response.urljoin(response.xpath("//a[@class='downPage']/@href")[0].extract())
            print(next_page)
            if next_page is not None:
                time.sleep(random.randint(10, 30) / 10)
                yield scrapy.Request(next_page, callback=self.parse)
            else:
                print("爬取完毕,共%s张图片" % pic_num)
        except:
            pass

    def pic_detail(self, response):
        pixel = response.xpath("//div[@class='photo-info']/div/div/ul")
        size = response.xpath("//div[@class='photo-info']/div/div[@class='infor']")
        item = response.meta
        try:
            pixel = pixel.xpath("li/span[@class='set-width2']/text()")[0].extract()
            i = re.search(r'[\D]*(\d*)[\D]*(\d*).*', pixel)
            i1 = i.group(1)
            i2 = i.group(2)
            item['pixel'] = str(i1 + "x" + i2)
        except:
            item['pixel'] = '未获取到'
        try:
            item['size'] = size.xpath("p/span/text()")[4].extract().strip()
        except:
            item['size'] = '未获取到'
        yield item
